package openc.ui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.beans.PropertyVetoException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

public class JWindowsListBar extends JPanel 
        implements InternalFrameListener, ContainerListener, ActionListener {
    public JWindowsListBar(JDesktopPane desktop) {
        super(new FlowLayout(FlowLayout.LEADING));
        this.buttons = new HashMap(5);
        this.desktop = desktop;
        this.desktop.addContainerListener(this);

		for (JInternalFrame window : desktop.getAllFrames()) {
            addWindow(window);
		}
    }

    public void internalFrameOpened(InternalFrameEvent e) {
    }

    public void internalFrameClosing(InternalFrameEvent e) {
    }

    public void internalFrameClosed(InternalFrameEvent e) {
        JInternalFrame window = e.getInternalFrame();
        JToggleButton button = buttons.get(window);
        this.remove(button);
        this.buttons.remove(window);
        this.getLayout().layoutContainer(this.getParent());
    }

    public void internalFrameIconified(InternalFrameEvent e) {
        e.getInternalFrame().setVisible(false);
    }

    public void internalFrameDeiconified(InternalFrameEvent e) {
        JInternalFrame window = e.getInternalFrame();
        window.setVisible(true);
    }

    public void internalFrameActivated(InternalFrameEvent e) {
        JInternalFrame window = e.getInternalFrame();
        Set<Map.Entry<JInternalFrame, JToggleButton>> set = buttons.entrySet();
        for (Map.Entry<JInternalFrame, JToggleButton> entry : set) {
            if (entry.getKey() != window)
                entry.getValue().setSelected(false);
            else
                entry.getValue().setSelected(true);
        }
    }

    public void internalFrameDeactivated(InternalFrameEvent e) {
    }

    public void componentAdded(ContainerEvent e) {
        if (e.getChild() instanceof JInternalFrame) {
            JInternalFrame window = (JInternalFrame)e.getChild();
            addWindow(window);
        }
    }

    public void componentRemoved(ContainerEvent e) {
    }

    protected void addWindow(JInternalFrame window) {
        if (!buttons.containsKey(window)) {
            JToggleButton button = new JToggleButton(window.getTitle());
            button.addActionListener(this);
            this.buttons.put(window, button);
            this.add(button);
            window.addInternalFrameListener(this);
        }
    }

    private HashMap<JInternalFrame, JToggleButton> buttons;
    private JDesktopPane desktop;

    public void actionPerformed(ActionEvent e) {
        JToggleButton button = (JToggleButton)e.getSource();
        JInternalFrame window;
        Set<Map.Entry<JInternalFrame, JToggleButton>> set = buttons.entrySet();
        for (Map.Entry<JInternalFrame, JToggleButton> entry : set) {
            if (entry.getValue() == button)
            {
                try {
                    window = entry.getKey();
                    if (!window.isVisible()) {
                        window.setVisible(true);
                        window.setIcon(false);
                    }

                    window.setSelected(button.isSelected());
                }
                catch (PropertyVetoException ex) {
                    System.err.println(ex.getMessage());
                }
            }
        }
    }
}
